package com.tencent.qcloud.qiaoqiao.ui.activity;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tencent.qcloud.qiaoqiao.R;
import com.tencent.qcloud.qiaoqiao.ui.BaseActivity;
import com.tencent.qcloud.qiaoqiao.ui.tools.utilCode.ActivityUtils;

import butterknife.BindView;
import butterknife.ButterKnife;


public class WebViewActivity extends BaseActivity {
    @BindView(R.id.webView)
    WebView webView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ProgressBar)
    android.widget.ProgressBar ProgressBar;
    String url, title;

    public static void actionStart(Context context, String url) {
        Intent intent = new Intent(context, WebViewActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        url = getIntent().getStringExtra("url");
        title = getIntent().getStringExtra("name");
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);

        initView();
        loadUrl();
    }

    private void loadUrl() {
        ProgressBar.setVisibility(View.VISIBLE);
        webView.loadUrl(url);
    }

    private void initView() {
        refreshLayout.setEnableLoadMore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
              //  loadUrl();
            }
        });

        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setTextZoom(100);
       // webView.setInitialScale(100);
        WebChromeClient chromeClient = new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (!TextUtils.isEmpty(WebViewActivity.this.title)){
                    return;
                }

            }
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // 提前加载完成
                if(newProgress>=50){
                    refreshLayout.finishRefresh();
                    ProgressBar.setVisibility(View.GONE);
                }
            }
        };

        // 设置setWebChromeClient对象
        webView.setWebChromeClient(chromeClient);
        webView.addJavascriptInterface(new  JsInterface(), "webkitBridge");
        webView.setWebViewClient(new WebViewClientNormal());
    }


    private class WebViewClientNormal extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
           // view.loadUrl(url);
            return false;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            //Log.e(TAG, "onReceivedError: ----url:" + error.getDescription());
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                return;
            }
          //  ToastUtils.showShort(getString(R.string.system_error)+failingUrl);
        }


        @TargetApi(Build.VERSION_CODES.M)
        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
          //  ToastUtils.showShort(getString(R.string.system_error)+error);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            refreshLayout.finishRefresh();
            ProgressBar.setVisibility(View.GONE);
        }
    }

    public class JsInterface {
        @JavascriptInterface
        public void handleJS(int type) {
            Intent intent = new Intent(WebViewActivity.this,MainUIActivity.class);
            intent.putExtra("type",type);
            ActivityUtils.startActivity(intent);
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {//当返回按键被按下
            webView.goBack();
            return true;
        } else {
            finish();
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }
}
